//
//  ViewController.m
//  24-加密解密
//
//  Created by 殷殷明静 on 16/8/16.
//  Copyright © 2016年 殷明静. All rights reserved.
//

#import "ViewController.h"
#import "NSString+YMJMD5.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *jiaMiText;
@property (weak, nonatomic) IBOutlet UITextField *jiemiText;
@property (weak, nonatomic) IBOutlet UIButton *btn;
- (IBAction)jieMiBtn:(id)sender;
- (IBAction)btnAction:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)jieMiBtn:(id)sender {
    // 加密解密是单向的,不能将加密的字符串拿出解密,反之亦然
    NSString *MD5 = @"qwert";
    NSString *newStr = [MD5 stringWithMD5];
    NSLog(@"加密结果%@",newStr);
//    newStr = [newStr stringWithMD5];
    // 解密decrypt
    NSString *str = @"http://tool.zzblo.com/api/md5/decrypt";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [[NSString stringWithFormat:@"secret=%@",newStr] dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            
            _jiaMiText.text = dic[@"text"];
        });
        
    }];
    [task resume];
}

- (IBAction)btnAction:(id)sender {
    // 加密encrypt
    NSString *str = @"http://tool.zzblo.com/api/md5/encrypt";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [[NSString stringWithFormat:@"text=%@",_jiaMiText.text] dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"%@",dic);
            _jiemiText.text = dic[@"secret32"];
            _jiaMiText.text = nil;
        });
        
    }];
    [task resume];
}
@end
